<section class="content-header" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i> 首页</a></li>
        <li><a href="#">销售机会</a></li>
        <li class="active">客户来源</li>
    </ol>
</section>
<!-- Main content -->
<section class="content">
    <div class="jax-box">
        <form id="formSearch" class="form-horizontal form-search">
            <div class="form-group">
                <div class="col-md-4 col-sm-6">
                    <label class="control-label  col-xs-4" for="chcSource">来源:</label>
                    <div class="col-xs-8">
                        <input type="text" class="form-control" placeholder="" id="chcSource">

                    </div>
                </div>
                <div class="col-md-4 col-sm-6">
                    <label class="control-label  col-xs-4" for="chcCustName">名称:</label>
                    <div class="col-xs-8">
                        <input type="text" class="form-control" placeholder="" id="chcCustName">

                    </div>
                </div>
                <div class="col-md-3 col-sm-6">
                    <label class="control-label  col-xs-4" for="chcStatus">机会:</label>
                    <div class="col-xs-8">
                        <select name="chcStatus" id="chcStatus" class="form-control m-b">
                            <option  value="0">全部订单</option>
                            <option  value="1">已指派</option>
                            <option  value="2">待指派</option>
                        </select>
                    </div>
                </div>
                <shiro:hasPermission name="selChance:list">
                    <div class="col-md-1 col-sm-6 " style="text-align:left;">
                        <button type="button" id="btn_query" class="btn btn-info">查询</button>
                    </div>

                </shiro:hasPermission>
            </div>
        </form>
    </div>
    <div class="jax-box jax-box-table">
        <div id="toolbar" class="btn-group">
            <shiro:hasPermission name="selChance:add">
                <button id="btn_add" type="button" class="btn btn-primary" data-toggle="modal" data-target="#userModal">
                    <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增
                </button>
            </shiro:hasPermission>
            <shiro:hasPermission name="selChance:batchDelete">
                <button id="btn_batch_delete" type="button" class="btn btn-danger">
                    <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>批量删除
                </button>
            </shiro:hasPermission>
        </div>
        <table id="table"></table>
    </div>
</section>
<!-- 用户新增模态框 -->
<div id="userModal" class="modal fade" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <!--        selChance新增按钮页面-->
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">新增销售机会</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" id="selChanceForm">
                    <div class="box-body">
                        <div class="form-group">
                            <label class="col-sm-3 control-label">联系人编号 <span style="color: red">*</span>:</label>
                            <div class="col-sm-8">
                                <input name="chcId" class="form-control" require="必填项" placeholder="">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">机会来源 <span style="color: red">*</span>:</label>
                            <div class="col-sm-8">
                                <input name="chcSource" class="form-control" require="必填项" placeholder="">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">客户名称 <span style="color: red">*</span>:</label>
                            <div class="col-sm-8">
                                <input name="chcCustName" class="form-control"  require="必填项" placeholder="">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">概要 :</label>
                            <div class="col-sm-8">
                                <input name="chcTitle" class="form-control" placeholder="">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">成功机率 :</label>
                            <div class="col-sm-8">
                                <input name="chcRate" class="form-control" placeholder="">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">联系人 :</label>
                            <div class="col-sm-8">
                                <input name="chcLinkman" class="form-control" placeholder="">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">联系人电话 :</label>

                            <div class="col-sm-8">
                                <input name="chcTel" class="form-control" placeholder="">
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-3 control-label">联系人性别 :</label>
                            <div class="col-sm-8">
                                男:<input  name="chcGender" checked type="radio" value="男">
                                女:<input  name="chcGender" type="radio" value="女">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">机会描述 :</label>

                            <div class="col-sm-8">
                                <input name="chcDesc" class="form-control" placeholder="">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">创建人编号 :</label>

                            <div class="col-sm-8">
                                <input name="chcCreateId" class="form-control" placeholder="">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">创建人姓名 :</label>

                            <div class="col-sm-8">
                                <input name="chcCreateName" class="form-control" placeholder="">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">指派给的人编号 :</label>
                            <div class="col-sm-8">
                                <input name="chcDueId" class="form-control" placeholder="">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">指派给的人姓名 :</label>
                            <div class="col-sm-8">
                                <input name="chcDueName" class="form-control" placeholder="">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">销售机会状态 :</label>
                            <div class="col-sm-8">
                                <select name="chcStatus">
                                    <option value="1">已指派</option>
                                    <option value="2">未指派</option>
                                </select>
                            </div>
                        </div>


                    </div>
                    <div class="box-footer">
                        <div class="pull-right">
                            <button type="button" id="saveUser" class="btn btn-info">确定</button>
                            <button type="button" data-dismiss="modal" class="btn btn-default">取消</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<!--&lt;!&ndash; 用户详情模态框 &ndash;&gt;-->
<div id="userDetailModal" class="modal fade" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">营销信息更改</h4>
            </div>
            <div class="modal-body">
                <div id="userOpenWindow">
                </div>
            </div>
        </div>
    </div>
</div>
<div id="userDetailModal2" class="modal fade" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">营销信息详情</h4>
            </div>
            <div class="modal-body">
                <div id="userOpenWindow2">
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    var editFlag = "[[${@perms.hasPerm('selChance:edit')}]]";
    var deleteFlag = "[[${@perms.hasPerm('selChance:delete')}]]";
    var edit2Flag = "[[${@perms.hasPerm('selChance:edit2')}]]";
    //删除原来数据转换为客户
    var delete2Flag = "[[${@perms.hasPerm('selChance:delete2')}]]";
    var idsStr;//批量用户id
    var roleIdStr;//分配的角色
    // var selectRoleIds;
    var userIdChecked;
    // var loadRoleCount = 0;
    var columns = [
        {checkbox: true},
        {
            field: 'chcSource',
            title: '机会来源',
            align: "center"
        },{
            field: 'chcCustName',
            title: '客户名称',
            align: "center"
        },
        {
            field: 'chcRate',
            title: '成功机率',
            align: "center"
        },
        {
            field: 'chcLinkman',
            title: '联系人',
            align: "center"
        },
        {
            field: 'chcTel',
            title: '联系人电话',
            align: "center"
        },
        {
            field: 'chcBirthday',
            title: '联系人生日',
            align: "center"
        },
        {
            field: 'chcGender',
            title: '联系人性别',
            align: "center",
        },
        {
            field: 'chcDueName',
            title: '指派人姓名',
            align: "center"
        },
        {
            field: 'chcStatus',
            title: '执行效果',
            align: "center",
            formatter: function (value, row, index) {
                if(row.chcStatus == 1){
                    return  "已开发";
                }
                if (row.chcStatus == 2){
                    return "未开发";
                }
            }
        },
        {
            field: 'operation',
            title: '操作',
            align: "center",
            formatter: function (value,
                                 row, index) {
                var edit2 = edit2Flag == "true" ? '<a class="table-btn table-btn-success" href="javascript:void(0)" onclick="editUser2(' + row.id + ')">详情</a>' : '';
                var edit = editFlag == "true" ? '<a class="table-btn table-btn-info" href="javascript:void(0)" onclick="editUser(' + row.id + ')">编辑</a>' : '';
                var del = deleteFlag == "true" ? '<a  class="table-btn table-btn-danger" href="javascript:void(0)" onclick="deleteUser(' + row.id + ')">删除</a>' : '';
                var del2 = delete2Flag == "true" ? "<a  class='table-btn table-btn-danger' href='javascript:void(0)' onclick='deleteUser2("+ row.id + ",\""+row.chcTel + "\",\""+row.chcLinkman+"\","+row.chcCreateId+",\""+row.chcCreateName + "\")'>转换为客户</a>": '';
                return edit  + del+edit2+del2;
            }
        }];
    var options = {
        id: "#table",
        url: '/selChance/list',
        columns: columns,
        toolbar: '#toolbar',
        showRefresh: true,
        queryParams: queryParams
    }
    Core.initTable(options);

    /*查询用户参数*/
    function queryParams(params) {
        var temp = { //这里的键的名字和控制器的变量名必须一致，这边改动，控制器也需要改成一样的
            limit: params.limit, //页面大小
            offset: params.offset, //页码
            chcSource: $("#chcSource").val(),
            chcCustName: $("#chcCustName").val(),
            chcStatus: $("#chcStatus").val(),
        };
        return temp;
    }

    /*查询角色参数*/
    function queryRoleParams(params) {
        var temp = {
            id: userIdChecked
        };
        return temp;
    }

    // /*编辑*/
    function editUser(id) {
        Core.load("#userOpenWindow", "/selChance/edit?id=" + id, function () {
            $("#userDetailModal").modal("show");
        }, 2);
    }

    function editUser2(id) {
        Core.load("#userOpenWindow2", "/selChance/edit2?id=" + id, function () {
            $("#userDetailModal2").modal("show");
        }, 2);
    }
    //
    /*删除*/
    function deleteUser(id) {
        Core.confirm("确定删除该联系人？", function () {
            Core.postAjax("/selChance/delete", {"id": id}, function (data) {
                if (data.status == 200) {
                    Core.refreshTable("#table");
                }
                layer.msg(data.msg);
            }, "get")
        })
    }
 // 更改为客户
    function deleteUser2(id,chcTel,chcLinkman,chcCreateId,chcCreateName) {
        //判断该联系人是否信息全面
        if (id == null  || chcLinkman == "null" || chcLinkman == "" || chcCreateId == null || chcCreateName == "null" || chcCreateName == "" ){
            layer.msg("联系人信息不全!!!!");
            Core.load("#userOpenWindow", "/selChance/edit?id=" + id, function () {
                $("#userDetailModal").modal("show");
            }, 2);
        }else{
            Core.confirm("确定更改为用户吗？", function () {
                paramss={
                    //客户表  销售机会表
                    custName:chcLinkman,
                    custTel:chcTel,
                    custManangerId:chcCreateId,
                    custManangerName:chcCreateName,
                    // custBirthday:chcBirthday,
                    // custGender:chcGender,
                }
                Core.postAjax("/customer/insert", paramss, function (data2) {
                    if (data2.status == 200) {
                        Core.postAjax("/selChance/delete2", {"id":id,"lkmName":chcLinkman}, function (data) {
                            if (data2.custName != data.chcLinkman) {
                                Core.refreshTable("#table");
                            }else{
                                Core.refreshTable("#table");
                            }
                        }, "get")
                    }
                })

            })
        }

    }

    $(function () {
        /*查询*/
        $("#btn_query").click(function () {
            Core.refreshTable("#table");
        });
        //     /*新增*/
        $("#btn_add").click(function () {
            Core.clearError("#selChanceForm");
        });
        /*批量删除*/
        $("#btn_batch_delete").click(function () {
            var checkedRows = Core.selectMutiData("#table");
            if (checkedRows) {
                Core.confirm("确定删除选中的" + checkedRows.length + "条记录？", function () {
                    idsStr = "";
                    $.each(checkedRows, function (i, item) {
                        idsStr += (item.id + ",");
                    })
                    idsStr = idsStr.substring(0, idsStr.length - 1);
                    Core.postAjax("/selChance/batch/delete", {"idsStr": idsStr}, function (data) {
                        if (data.status == 200) {
                            Core.refreshTable("#table");
                        }
                        layer.msg(data.msg);
                    }, "get")
                })
            }
        });

        /*保存用戶 新增 确定*/
        $("#saveUser").click(function () {
            if (doValidForm(selChanceForm)) {
                Core.mask("#saveUser");
                Core.postAjax("/selChance/add", $("#selChanceForm").serialize(), function (data) {
                    Core.unmask("#saveUser");
                    if (data.status == 200) {
                        $("#userModal").modal("hide");
                        $("#selChanceForm")[0].reset();
                        Core.refreshTable("#table")
                    }
                    layer.msg(data.msg);
                })
            }
        });
    });

</script>